﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.Controls
{
    public partial class DDL_Category : System.Web.UI.UserControl
    {
        /// <summary>
        /// CSS属性
        /// </summary>
        public string ClassName { get; set; }
        /// <summary>
        /// 选中的值
        /// </summary>
        public string SelectedValue { get {
            return tb_Category.Text;
        }
            set { _SelectedValue = value; }
        }
        private string _SelectedValue = "";
        BLL.Category bllCategory = new BLL.Category();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //ddl1.Attributes.Add("class", ClassName);
                //ddl2.Attributes.Add("class", ClassName);
                //ddl3.Attributes.Add("class", ClassName);
                List<Model.Category> listModelCategory1 = bllCategory.GetModelList("FatherCategoryID=0 order by Rank");
                ddl1.DataSource = listModelCategory1;
                ddl1.DataTextField = "Name";
                ddl1.DataValueField = "id";
                ddl1.DataBind();
                if (DataTypePlus.requestInt( _SelectedValue)>0) Default_DDL3(DataTypePlus.requestInt(_SelectedValue));
                else if (ddl1.Items.Count > 0)
                {
                    ddl1.SelectedIndex = 0;
                    DDL1_changed();
                }
            } 
        }
        void Default_DDL3(int CategoryID3_)
        {
            Model.Category model3 = bllCategory.GetModel(CategoryID3_);
            string[] id_TreeA=model3.CategoryID_Tree.Split('|');
            ddl1.Value = id_TreeA[0];
            bind_DDL2(DataTypePlus.requestInt(id_TreeA[0]), id_TreeA[1]);
            ddl3.Value = CategoryID3_.ToString();
            tb_Category.Text = CategoryID3_.ToString();
        }
        void bind_DDL2(int CategoryID1_,string DefaultVal_)
        {
            List<Model.Category> listModelCategory2 = bllCategory.GetModelList("FatherCategoryID=" + CategoryID1_ + " order by Rank");
            ddl2.DataSource = listModelCategory2;
            ddl2.DataTextField = "Name";
            ddl2.DataValueField = "id";
            ddl2.DataBind();
            if (ddl2.Items.Count > 0 && DefaultVal_ != "")
            {
                try { ddl2.Value = DefaultVal_; DDL2_changed(); }
                catch (InvalidCastException e) { }
            }
        }
        
        //3
        void bind_DDL3(int CategoryID2_, string DefaultVal_)
        {
            List<Model.Category> listModelCategory3 = bllCategory.GetModelList("FatherCategoryID=" + CategoryID2_ + " order by Rank");
            ddl3.DataSource = listModelCategory3;
            ddl3.DataTextField = "Name";
            ddl3.DataValueField = "id";
            ddl3.DataBind();
            if (ddl3.Items.Count > 0 )
            {
                if (DefaultVal_ != "")
                {
                    try { ddl3.Value = DefaultVal_; }
                    catch (InvalidCastException e) { }
                }
                else ddl3.SelectedIndex = 0;
                tb_Category.Text = ddl3.Value;
            }
            
        }
        
        
        void DDL1_changed()
        {
            bind_DDL2(DataTypePlus.requestInt(ddl1.Value), "");
            if (ddl2.Items.Count > 0){
                ddl2.SelectedIndex = 0;
                DDL2_changed();
            }
        }
       
        void DDL2_changed()
        {
            bind_DDL3(DataTypePlus.requestInt(ddl2.Value), "");
            if (ddl3.Items.Count > 0) ddl3.SelectedIndex = 0;
        }










        void AddItems(int fatherCategoryID_)
        {
            foreach (Model.Category category in bllCategory.GetModelList("FatherCategoryID=" + fatherCategoryID_ + " order by Rank"))
            {
                int sonLevel = category.CategoryID_Tree.Length - category.CategoryID_Tree.Replace("|", "").Length - 1;
                string itemText = StringN("　", sonLevel) + category.Name;
                ddl1.Items.Add(new ListItem(itemText, category.id.ToString()));
                AddItems(category.id);
            }
        }
        string StringN(string str_, int n_)
        {
            string returnVal = "";
            for (int i = 0; i < n_; i++) returnVal += str_;
            return returnVal;
        }
    }
    
}